package org.example.io;

import java.io.*;
import java.nio.charset.Charset;

public class FileTool {

    public static final char Extension_Separator = '.';
    public static final char Unix_Separator='/';
    public static final char Windows_Separator='\\';

    public static String getExtension(String filename){
        // D:\123.txt
        // windows下默认反斜线\作为路径分隔符，但也支持正斜线/。
        filename=filename.replace(Windows_Separator,Unix_Separator);
        final int extensionPos=filename.lastIndexOf(Extension_Separator);
        return filename.substring(extensionPos+1);
    }
    /**
    * a/b/c.txt --> c.txt
    * 原理：filename.substring(indexOfLastSeparator + 1)
    */

    public static String getName(String filename){
        filename = filename.replace(Windows_Separator,Unix_Separator);
        final int lastUnixPos=filename.lastIndexOf(Unix_Separator);
        return filename.substring(lastUnixPos+1);
    }

    public static File create(String path,boolean isDir) throws IOException {
        File file = new File(path);
        if(isDir){
            if(file.exists()){
                if(!file.isDirectory()){
                    final String message=
                            "File"
                                    +path
                                    +"exists and is"
                                    +"not a directory.Unable to create directory";
                    throw new IOException(message);
                }
                return file;
            }
            else{
                if(!file.mkdirs()){
                    if(!file.isDirectory()){
                        final String message =
                                "Unable to create directory"+path;
                        throw new IOException(message);
                    }
                }
                return file;
            }
        }
        else{
            file.createNewFile();
            if(!file.isFile()){
                final String message=
                        "Unable to create file"+path;
                throw new IOException(message);
            }
            return file;
        }
    }

    public static void forceDelete(final File file) throws IOException {
        if(file.isDirectory()){
            deleteDirectory(file);
        }
        else{
            if(!file.delete()){
                if(!file.exists()){
                    return;
                }
                else{
                    final String message=
                            "Unable to delete file:"+file;
                    throw new IOException(message);
                }
            }
        }
    }

    public static void cleanDirectory(final File directory) throws IOException {
        final File[] files=directory.listFiles();
        IOException exception=null;
        for(final File file:files){
            try {
                forceDelete(file);
            } catch (IOException e) {
                exception=e;
            }
        }
        if(null!=exception){
            throw exception;
        }

    }


    public static void deleteDirectory(final File directory) throws IOException {
        if(!directory.exists()){
            return;
        }
        cleanDirectory(directory);
        if(!directory.delete()){
            if(directory.exists()){
                final String message=
                        "Unable to delete directory"+directory;
                throw new IOException(message);
            }
        }
    }

    public static void writeByteArrayToFile(final File file,final byte[] data,final boolean append) throws IOException {
        OutputStream out = null;
        out =new FileOutputStream(file,append);
        out.write(data,0,data.length);
        out.close();
        //IOTool.closeQuietly(out);

    }
    /**
     * 原理：FileOutputStream output.write
     * 强烈建议文件使用utf8
     * @throws IOException
     */
    public static void writeStringToFile(final File file, final String data, final Charset encoding,final boolean append) throws IOException {
        OutputStream out = null;
        out = new FileOutputStream(file,append);
        IOTool.write(data,out,encoding);
        //out.write(data.getBytes(encoding));
        out.close();
    }

    public static String readFileToString(final File file,final Charset encoding) throws IOException {
        InputStream in=null;
        try {
            in = new FileInputStream(file);
            return in.toString();
        }finally {
            in.close();
        }
    }


}
